/*
 * #%L
 * java model
 * %%
 * Copyright (C) 2009 - 2011 prefabware.com Stefan Isele
 * %%
 * All Rights Reserved.
 * This program is dual-licensed under the GNU General Public license
 * or a commercial license.
 * You may select to use one or the other of these licenses.
 * To obtain a commercial license please contact Stefan Isele at www.stefan-isele.de
 * Any modifications to this file must keep this entire header intact.
 * Redistribution, except as permitted by the selected license, is prohibited.
 * #L%
 */
package com.prefabware.jmodel;

import com.prefabware.commons.QualifiedName;
import com.prefabware.jmodel.code.CodeBuffer;
import com.prefabware.jmodel.code.JCodeOptions;

/**
 * the declaration of a JType e.g. JClass, JInterface etc.
 * a JTypeDeclaration can have type arguments when it is extending a generic supertype
 * @author sisele_job
 * 
 */
public class JTypeDeclaration extends JDeclarationBase {

	public JTypeDeclaration(QualifiedName qualifiedName, JModifiers modifiers,
			JVisibility visibility, JType componentType) {
		super(qualifiedName, modifiers, visibility);
		this.componentType = componentType;
	}

	@Override
	public String toString() {
		return "JTypeDeclaration [componentType=" + componentType + ", name="
				+ qualifiedName + "]";
	}

	/**
	 * @return the dimensions of this type, if its an array
	 * 0=its not an array
	 */
	public int getArrayDim() {
		if (this.componentType != null) {
			return 1 + this.componentType.getDeclaration().getArrayDim();
		} else {
			return 0;
		}

	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((componentType == null) ? 0 : componentType.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		JTypeDeclaration other = (JTypeDeclaration) obj;
		if (componentType == null) {
			if (other.componentType != null)
				return false;
		} else if (!componentType.equals(other.componentType))
			return false;
		return true;
	}

	public JType getType() {
		return this.getDeclaringType();
	};

	private JType componentType;

	public JType getComponentType() {
		return componentType;
	}

	/* 
	 * returns the declaration of the type, as used in Java code
	 * e.g. <code> public class Customer </code>
	 * the class name is allways the simple name, independent of parameter options
	 * because in declarations allways the unquallified name is used
	 */
	@Override
	public String asCode(JCodeOptions options) {
		CodeBuffer buf = new CodeBuffer(options);
		buf.append(this.getVisibilityAndModifier());
		buf.append(this.getType().getJavaKeyword());
		//allways use the simple name in declarations
		buf.append(this.getName());
		buf.setInsertLeadingSpace(false);
		
		// implements and extemds are not avalable here !!
		// superclasses have to append that
		return buf.toString().trim();
	}
}
